function serialize(_t)  --table转换成string
    if not DEBUG_VERSION then -- 发布版不需要打印table内容，直接返回以节省性能
        return ""
    end
    if _t == nil then
        return "[nil]"
    end
    local szRet = "{"
    local function doT2S(_i,_v)
        if "number" == type(_i)then
            szRet = szRet .. "[" .. _i .. "] = "
            if "number" == type(_v)then
                szRet = szRet .. _v .. ","
            elseif "string" == type(_v)then
                szRet = szRet .. '"' .. _v .. '"' .. ","
            elseif "table" == type(_v)then
                szRet = szRet .. serialize(_v).. ","
            else
                szRet = szRet .. "nil,"
            end
        elseif "string" == type(_i)then
            szRet = szRet .. '["' .. _i .. '"] = '
            if "number" == type(_v)then
                szRet = szRet .. _v .. ","
            elseif "string" == type(_v)then
                szRet = szRet .. '"' .. _v .. '"' .. ","
            elseif "table" == type(_v)then
                szRet = szRet .. serialize(_v).. ","
            else
                szRet = szRet .. "nil,"
            end
        end
    end
    table.foreach(_t,doT2S)
    szRet = szRet .. "}"
    return szRet
end

function serialized(_t,depth)  --table
    if not DEBUG_VERSION then -- 发布版不需要打印table内容，直接返回以节省性能
        return ""
    end
    if depth <= 0 then
        return "[table]"
    end
    if _t == nil then
        return "[nil]"
    end
    local szRet = "{"
    local function doT2S(_i,_v)
        if "number" == type(_i)then
            szRet = szRet .. "[" .. _i .. "] = "
            if "number" == type(_v)then
                szRet = szRet .. _v .. ","
            elseif "string" == type(_v)then
                szRet = szRet .. '"' .. _v .. '"' .. ","
            elseif "table" == type(_v)then
                szRet = szRet .. serialized(_v,depth-1).. ","
            else
                szRet = szRet .. "["..type(_v).."],"
            end
        elseif "string" == type(_i)then
            szRet = szRet .. '["' .. _i .. '"] = '
            if "number" == type(_v)then
                szRet = szRet .. _v .. ","
            elseif "string" == type(_v)then
                szRet = szRet .. '"' .. _v .. '"' .. ","
            elseif "table" == type(_v)then
                szRet = szRet .. serialized(_v,depth-1).. ","
            else
                szRet = szRet .. "["..type(_v).."],"
            end
        end
    end
    table.foreach(_t,doT2S)
    szRet = szRet .. "}"
    return szRet
end

function serializedWithoutKey(_t,depth)  --table转字符串，且去掉key值
    if depth <= 0 then
        return "[table]"
    end
    if _t == nil then
        return "[nil]"
    end
    local szRet = "{"
    local function doT2S(_i,_v)
        if "number" == type(_i)then
            --szRet = szRet .. "[" .. _i .. "] = "
            if "number" == type(_v)then
                szRet = szRet .. _v .. ","
            elseif "string" == type(_v)then
                szRet = szRet .. '"' .. _v .. '"' .. ","
            elseif "table" == type(_v)then
                szRet = szRet .. serializedWithoutKey(_v,depth-1).. ","
            else
                szRet = szRet .. "["..type(_v).."],"
            end
        elseif "string" == type(_i)then
            --szRet = szRet .. '["' .. _i .. '"] = '
            if "number" == type(_v)then
                szRet = szRet .. _v .. ","
            elseif "string" == type(_v)then
                szRet = szRet .. '"' .. _v .. '"' .. ","
            elseif "table" == type(_v)then
                szRet = szRet .. serializedWithoutKey(_v,depth-1).. ","
            else
                szRet = szRet .. "["..type(_v).."],"
            end
        end
    end
    table.foreach(_t,doT2S)
    szRet = szRet .. "}"
    return szRet
end

function do_serialize(_t)  --table转换成string
    if _t == nil then
        return "[nil]"
    end
    local szRet = "{"
    local function doT2S(_i,_v)
        if "number" == type(_i)then
            szRet = szRet .. "[" .. _i .. "] = "
            if "number" == type(_v)then
                szRet = szRet .. _v .. ","
            elseif "string" == type(_v)then
                szRet = szRet .. '"' .. _v .. '"' .. ","
            elseif "table" == type(_v)then
                szRet = szRet .. do_serialize(_v).. ","
            else
                szRet = szRet .. "nil,"
            end
        elseif "string" == type(_i)then
            szRet = szRet .. '["' .. _i .. '"] = '
            if "number" == type(_v)then
                szRet = szRet .. _v .. ","
            elseif "string" == type(_v)then
                szRet = szRet .. '"' .. _v .. '"' .. ","
            elseif "table" == type(_v)then
                szRet = szRet .. do_serialize(_v).. ","
            else
                szRet = szRet .. "nil,"
            end
        end
    end
    table.foreach(_t,doT2S)
    szRet = szRet .. "}"
    return szRet
end

function unserialize(tab_str)  --string 转换成 table
    local t = type(tab_str)
    if t == "nil" or tab_str == "" then
        return nil
    elseif t == "number" or t == "string" or t == "boolean" then
        tab_str = tostring(tab_str)
    else
        error("can not unserialize a " .. t .. " type.")
    end
    tab_str = "return " .. tab_str
    local func = loadstring(tab_str)
    if func == nil then
        return nil
    end
    return func()
end

function split(s,delim)
    delim = delim or ","
    if type(delim)~= "string" then
        return
    end

    

    local start = 1
    local t = {}

    if string.len(delim)==0 then
        -- 输入为''时 直接分割 zjb 2015年4月16日 添加
        for start = 1,string.len(s),1 do
            table.insert(t,string.sub(s,start,start))
        end
        return t 
    end

    while true do
    local pos = string.find(s,delim,start,true)-- plain find
        if not pos then
          break
        end

        table.insert(t,string.sub(s,start,pos - 1))
        start = pos + string.len(delim)
    end
    table.insert(t,string.sub(s,start))

    return t
end

function distance(p1,p2)
    local delta_x = p1.x - p2.x
    local delta_y = p1.y - p2.y
    return math.sqrt(delta_x * delta_x + delta_y * delta_y)
end

function contain(tbl,item)
    for _,tbl_item in pairs(tbl)do
        if tbl_item == item then
            return true
        end
    end
    return false
end

function deepCopy(object)
    local lookup_table = {}
    local function _copy(object)
        if type(object)~= "table" then
            return object
        elseif lookup_table[object] then
            return lookup_table[object]
        end
        local new_table = {}
        lookup_table[object] = new_table
        for index,value in pairs(object)do
            new_table[_copy(index)] = _copy(value)
        end
        return setmetatable(new_table,getmetatable(object))
    end

    return _copy(object)
end

function dicTableCount(dic)
    local count = 0
    for key, value in pairs(dic) do
        count = count + 1
    end

    return count
end



